var express = require('express');
var axios = require('axios');
const multer = require('multer')
var router = express.Router();


var examNum = 0
var Works = new Array()
var testWordIds
var examWordId
var promates
var promatesIndex = 0

/**
 * @api {get} /admin/addpage 渲染页面
 * @apiName 渲染页面
 * @apiGroup page
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get('/',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, function (req, res, next) {
    // var testWordId = req.params.id;
    var { testWordId, id } = req.query
    console.log(id)
    examWordId = id
    testWordIds = testWordId

    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/ExamWork/getExamWorkById',
        params: {
            Id: id
        },
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("获取试卷信息成功")
                examNum = response.data.data.promates.length
                promates = response.data.data.promates
                console.log(promates)
                console.log(promates[promatesIndex])
                res.render('admin/addpage', {
                    list: { examNum: examNum, promates: promates[promatesIndex] }
                });
                res.send({ err: 0, msg: "获取试卷信息成功", data: response.data.data })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "获取试卷信息失败" })
                console.log("获取试卷信息失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误，请刷新页面" })
            console.log("服务器错误")
        });
})

/**
 * @api {get} /admin/addpage 渲染二级出题页面
 * @apiName 渲染二级出题页面
 * @apiGroup page
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get('/:testWordId/:promates/detail', function (req, res, next) {
    // var testWordId = req.params.id;
    testWordId = req.params.testWordId;
    var promatess = req.params.promates;
    res.render('admin/addpage', {
        list: { examNum: examNum, promates: promatess }
    });
})

/**
 * @api {get} /admin/addpage/update 回显出题页面
 * @apiName 回显出题页面
 * @apiGroup page
 *
 * @apiParam {String} id 题目id.
 * 
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.get('/update',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, function (req, res, next) {
    let { id } = req.query
    console.log(id)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Subject/getSubjectsById',
        params: {
            id: id,
            Id: id
        },
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("获取试卷信息成功")
                res.render('admin/updatepage', {
                    list: response.data.data
                });
                res.send({ err: 0, msg: "获取试卷信息成功", data: response.data.data })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "获取试卷信息失败" })
                console.log("获取试卷信息失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误，请刷新页面" })
            console.log("服务器错误")
        });
})

/**
 * @api {post} /admin/addpage/addexamworks 上传题目
 * @apiName 发布页面
 * @apiGroup page
 *
 * @apiParam {String} answerA A选项.
 * @apiParam {String} answerB B选项.
 * @apiParam {String} answerC C选项.
 * @apiParam {String} answerD D选项.
 * @apiParam {String} picUrl 图片地址.
 * @apiParam {String} relAnswer 正确答案.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/addexamworks', (req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
},(req, res, next) => {
    console.log(req.body)
    let { answerA, answerB, answerC, answerD, picUrl, isMany, relAnswer, subjectValue, remark, Promates } = req.body

    console.log(examNum)
    console.log(isMany)
    console.log(relAnswer)
    let work = {
        answerA: answerA,
        answerB: answerB,
        answerC: answerC,
        answerD: answerD,
        picUrl: picUrl,
        relAnswer: relAnswer,
        isMany: isMany,
        testWordId: testWordIds,
        examWordId: examWordId,
        subjectValue: subjectValue,
        remark: remark,
        subjectNumber: Promates
    }
    Works.push(work)
    if (examNum > 1) {
        //目前还没完成出题

        console.log(Works)
        examNum--
        promatesIndex++
        console.log(promates)
        console.log(promatesIndex)
        console.log(promates[promatesIndex])

        res.send({ err: 0, msg: "本题已保存，请继续出题完成所有任务点，现在退出页面则之前保存的题目无效", data: { url: "/admin/addpage/" + testWordIds + "/" + promates[promatesIndex] + "/detail" } })
    } else {
        console.log(JSON.stringify(Works))
        // Works = JSON.stringify(Works)
        var config = {
            method: 'post',
            baseURL: 'http://localhost:9023',
            url: '/ExamWork/addExamWorks',
            data: Works,
        }
        axios(config)
            .then(function (response) {
                // console.log(response.data);
                if (response.data.code == 0) {
                    console.log("获取试卷信息成功")
                    res.send({ err: 0, msg: "出题任务完成", data: { url: "/admin/addpagelist" } })
                    Works.length = 0
                } else {
                    // console.log(response);
                    res.send({ err: -1, msg: "获取试卷信息失败" })
                    console.log("获取试卷信息失败")
                }
            })
            .catch(function (error) {
                console.log(error);
                res.send({ err: -1, msg: "服务器错误，请刷新页面" })
                console.log("服务器错误")
            });
    }
})

/**
 * @api {post} /admin/addpage/updateexamworks 更新修改题目
 * @apiName 更新修改题目
 * @apiGroup page
 *
 * @apiParam {String} id 题目id.
 * @apiParam {String} answerA A选项.
 * @apiParam {String} answerB B选项.
 * @apiParam {String} answerC C选项.
 * @apiParam {String} answerD D选项.
 * @apiParam {String} picUrl 图片地址.
 * @apiParam {String} relAnswer 正确答案.
 * @apiParam {String} subjectNumber 题号.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/updateexamworks',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, (req, res, next) => {
    console.log(req.body)
    let { id, answerA, answerB, answerC, answerD, picUrl, isMany, relAnswer, subjectValue, remark, subjectNumber, testWordId, examWordId } = req.body

    // console.log(examNum)
    // console.log(isMany)
    // console.log(relAnswer)
    let work = {
        id: id,
        answerA: answerA,
        answerB: answerB,
        answerC: answerC,
        answerD: answerD,
        picUrl: picUrl,
        relAnswer: relAnswer,
        isMany: isMany,
        testWordId: testWordId,
        examWordId: examWordId,
        subjectValue: subjectValue,
        remark: remark,
        subjectNumber: subjectNumber
    }
    // Works.push(work)

    // Works = JSON.stringify(Works)
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Subject/updateSubjectById',
        params: work,
    }
    axios(config)
        .then(function (response) {
            // console.log(response.data);
            if (response.data.code == 0) {
                console.log("修改成功")
                res.send({ err: 0, msg: "修改成功", data: { url: "/admin/endpagelist" } })
            } else {
                // console.log(response);
                res.send({ err: -1, msg: "修改失败", data: { url: "/admin/endpagelist" } })
                console.log("修改失败")
            }
        })
        .catch(function (error) {
            console.log(error);
            res.send({ err: -1, msg: "服务器错误，请刷新页面" })
            console.log("服务器错误")
        });
})


module.exports = router;
